package com.gufe.controller.admin.joinArmy;

import com.gufe.dto.adminJoinArmy.AddJoinArmyDTO;
import com.gufe.dto.adminJoinArmy.ArmyMemberDTO;
import com.gufe.dto.adminJoinArmy.JoinArmyInfoDTO;
import com.gufe.dto.adminJoinArmy.PageJoinArmyMemberDTO;
import com.gufe.entity.ArmyMember;
import com.gufe.mapper.admin.joinArmy.JoinArmyMapper;
import com.gufe.result.PageResult;
import com.gufe.result.Result;
import com.gufe.service.admin.joinArmy.JoinArmyService;
import com.gufe.vo.joinArmy.JoinArmyPersonInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/joinArmy")
@PreAuthorize("hasRole('ADMIN')")
public class JoinArmyController {

    @Autowired
    private JoinArmyMapper joinArmyMapper;
    @Autowired
    private JoinArmyService joinArmyService;

    //获取入伍总人数
    @GetMapping("/totalCount")
    public Result getTotalCount() {
        return Result.success(joinArmyMapper.getTotalCount());
    }

    //分页查询入伍人员信息
    @PostMapping("/members")
    public Result<PageResult> pageJoinArmyMember(@RequestBody PageJoinArmyMemberDTO pjamd){
        log.info("分页查询入伍成员信息第{}页,{}条数据",pjamd.getPage(),pjamd.getPageSize());
        PageResult  pageResult = joinArmyService.pageJoinArmyMember(pjamd);
        return Result.success(pageResult);
    }

    //新建成员
    @PostMapping("/newMember")
    public Result newJMember(@RequestBody ArmyMemberDTO armyMember){
        log.info("新增入伍成员{}",armyMember);
        joinArmyService.newJoinArmyMember(armyMember);
        return Result.success();
    }

    //将预储或预征班成员添加到入伍成员
    @PostMapping("/addJoinArmy")
    public Result addJoinArmy(@RequestBody AddJoinArmyDTO addJoinArmyDTO){
        joinArmyService.addJoinArmyMember(addJoinArmyDTO);
        return Result.success();
    }

    //从预储预征获取入伍人员相关信息
    @PostMapping("/joinArmyInfo")
    public Result<List<ArmyMember>> getJoinArmyInfo(@RequestBody JoinArmyInfoDTO jaid){
        List<ArmyMember> list = joinArmyService.getJoinArmyInfo(jaid);
        return Result.success(list);
    }

    /**
     * 导入退伍人员信息表Excel
     **/
    @PostMapping( "/import-excel/army")
    public Result excelProTbZbzs(@RequestParam("file") MultipartFile file) {
        log.info("从Excel中导入入伍人员信息");
        String importResult = joinArmyService.importArmyData(file);
        return Result.success(importResult);

    }


    //获取个人详细信息
    @GetMapping("/personDetailInfo")
    public Result<JoinArmyPersonInfoVO> getPersonDetailInfo(String id){
        log.info("获取入伍人员个人信息id：{}",id);
        JoinArmyPersonInfoVO  joinArmyPersonInfoVO = joinArmyService.getPersonDetailInfo(id);
        return Result.success(joinArmyPersonInfoVO);
    }
}
